﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for MonThiDAL
/// </summary>
public class MonThiDAL
{
	
		public static object IsNull(object checkValue, object replaceValue)
    {
        if (checkValue.Equals(System.DBNull.Value) || checkValue == null)
            return replaceValue;
        return checkValue;
    }

    public static List<MonThiINFO> listAll()
    {
        string sql = "select * from MONTHI";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<MonThiINFO>(dr);
    }

    public static MonThiINFO GetInfo(int mamon)
    {
        string sql = "select * from MONTHI where MaMon ="+mamon+"";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<MonThiINFO>(dr);
    }

    public static int Insert(MonThiINFO obj)
    {
        obj.MaMon = GetMa();
        obj.MaMon = obj.MaMon + 1;
        string sql = "INSERT INTO MONTHI (MaMon,TenMon) VALUES ("+obj.MaMon+",N'"+obj.TenMon+"')";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Update(MonThiINFO obj)
    {
        string sql = "Update MONTHI set TenMon=N'" + obj.TenMon + "' Where mamon="+obj.MaMon+"";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Delete(int mamon)
    {
        string sql = "Delete from MONTHI where MaMon =" + mamon + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int GetMa()
    {
        string sql = "SELECT ISNULL(MAX(MaMon),0) FROM MONTHI";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        int ma = 0;
        if (dr.Read()) {
            ma = int.Parse(dr[0].ToString());
        }
        dr.Close();
        return ma;
    }
	
}